﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Tools;

namespace Punia
{
    [CommandAttribute(false)]
    public partial class create_master_key : BaseForm
    {
        public create_master_key()
            : base()
        {
        }

        public create_master_key(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();
        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            if (cbDrop.Checked)
            {
                sb.AppendLine("DROP MASTER KEY;");
                sb.AppendLine();
            }

            sb.AppendLine("CREATE MASTER KEY ENCRYPTION BY PASSWORD = '" + tbPass.Text + "';");
            sb.AppendLine();

            if (tbFileName.Text != string.Empty)
            {
                sb.AppendLine("BACKUP MASTER KEY TO FILE = '" + tbFileName.Text + "'");
                sb.AppendLine("   ENCRYPTION BY PASSWORD = '" + tbPass.Text + "';");
            }

            tbCommand.Text = sb.ToString();
        }

        private void tbPass_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbDrop_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbFileName_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void btnFilename_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            dialog.AddExtension = true;
            dialog.CheckPathExists = true;
            dialog.DefaultExt = ".cer";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                tbFileName.Text = dialog.FileName;
            }

        }
    }
}